<?php
class customer_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function updateSendMail($listMail){
        $arr=  explode(",", $listMail);
        foreach ($arr as $m){
            if($m!="" && $m!=null){
               $Emails[]=$m; 
            }
        }
        //$Emails = array('Frank', 'Todd', 'James');
        $this->db->where_in('Email', $Emails);
        $this->db->set('LastSend', 'NOW()', FALSE);
        $this->db->update('dtb_mail');
//        $query = "
//            UPDATE `dtb_mail` 
//            SET `LastSend`    =   now()
//            WHERE `Email` in ( $listMail )
//        ";
//        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function get($ID)
    {
        $where=array("CustomerID"=>$ID);
        $query = $this->db->get_where("dtb_customer",$where);
        return $query->result();
    }
    function get_($ID)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_customer` 
            WHERE `Delete` is null
                AND `CustomerID`='$ID'
        ");
        return $query->result();
    }
    function getByGroup($Group)
    {
        $query = $this->db->query("
            SELECT dtb_mail.*,`dtb_customer`.CustomerName
            FROM `dtb_mail` left join `dtb_customer` on `dtb_mail`.`Email`=`dtb_customer`.`Mail`
            WHERE 
                `dtb_mail`.`Type`='$Group'
                AND `dtb_mail`.`Delete` IS NULL
        ");
        return $query->result();
    }
    function delete($ID){
        $query = "
            UPDATE `dtb_customer` 
            SET `Delete` = now()
            WHERE `CustomerID`='$ID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restore($ID){
//        $query = "
//            UPDATE `dtb_customer` 
//            SET     `Delete`    =   null
//            WHERE   `CustomerID`        =   '$ID'
//        ";
//        $this->db->query($query);
        $this->db->set('Delete', 'NULL', FALSE);
        $this->db->where('CustomerID', $ID);
        $this->db->update('dtb_customer');
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function deleteMail($ID){
//        $query = "
//            UPDATE `dtb_customer` 
//            SET `Delete` = now()
//            WHERE `CustomerID`='$ID'
//        ";
//        $this->db->query($query);
        $this->db->set('Delete', 'NOW()', FALSE);
        $this->db->where('id', $ID);
        $this->db->update('dtb_mail');
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restoreMail($ID){
//        $query = "
//            UPDATE `dtb_customer` 
//            SET     `Delete`    =   null
//            WHERE   `CustomerID`        =   '$ID'
//        ";
//        $this->db->query($query);
        $this->db->set('Delete', 'NULL', FALSE);
        $this->db->where('id', $ID);
        $this->db->update('dtb_mail');
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function changeStatus($ID,$Status){
        $query = "
            UPDATE `dtb_customer` 
            SET `Status`    =   '$Status',
                `Update`    =   now()
            WHERE `CustomerID`='$ID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert('dtb_customer', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($CustomerID,$params){
        $this->db->where(array('CustomerID'=> $CustomerID,'Delete'=> null));
        //$this->db->where('delete', 'is null',FALSE);
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('dtb_customer', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateMail($Email,$params){
        $this->db->where(array('Email'=>$Email));
        $this->db->update('dtb_mail', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function gettotal_rows(){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `dviewcustommer`.*
            FROM `dviewcustommer`
            ";
        $configs["strWhere"]="
            WHERE true
            ";
        if($_SESSION["ADP-PRODUCT-DEL"]==false) $configs["strWhere"].=" AND `Delete` is null ";
        $configs["strOrderBy"]=" ORDER BY `dviewcustommer`.`Insert` DESC";
        $configs["fields"]=array(
            );
        $configs["usinglimit"]=true;
        $this->init($configs);
        return $this->jqxBinding();
//        $strQuery="
//            SELECT SQL_CALC_FOUND_ROWS `dviewcustommer`.*
//            FROM `dviewcustommer`
//            WHERE true 
//            ";
//        if($_SESSION["ADP-CUSTOMER-DEL"]==false) $strQuery.=" AND `Delete` is null ";
//        $this->__new($strQuery);
//        return $this->jqxBinding();
    }
    function gettotal_rows_mail(){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `dtb_mail`.*
            FROM `dtb_mail`
            ";
        $configs["strWhere"]="
            WHERE true
            ";
        $configs["strOrderBy"]=" ORDER BY `dtb_mail`.`Insert` DESC";
        $configs["fields"]=array(
            );
        $configs["usinglimit"]=true;
        $this->init($configs);
        return $this->jqxBinding();
//        $configs["strQuery"]="
//            SELECT SQL_CALC_FOUND_ROWS `dtb_mail`.*
//            FROM `dtb_mail`
//            ";
//        $configs["strWhere"]=" WHERE `Delete` is null ";
//        $configs["strOrderBy"]=" ORDER BY `Insert` DESC";
//        $this->init($configs);
//        
//        return $this->jqxBinding();
    }
    
    
}
?>
